const app = getApp()
const api = require('../../../api/index.js')
const util = require("../../../utils/util.js")
Page({
  /**
   * 页面的初始数据
   */
  data: {
    sex:"",
    age:'',
    goal:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      imgSrc: api.imgPathUrl
    })
    wx.showLoading({
      title: '加载中',
    })
    this.sportRims();
  },
  sportRims:function(){
    let data = {
      longitude: wx.getStorageSync('key').longitude,
      latitude: wx.getStorageSync('key').latitude
    }
    api.sportRim(data,(res)=>{
      console.log(res)
      this.setData({
        sportRimData: res.data.result
      })
      wx.hideLoading()
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },
  sex:function(e){  
    let inx = e.currentTarget.dataset.in
    let id = e.currentTarget.dataset.id
    if (id != this.data.sex) {
    this.data.sportRimData.apply_sex.forEach((item,index) =>{
      let key = `sportRimData.apply_sex[${index}].active`
      this.setData({
        [key]: false
      })
    })
    this.setData({
      [`sportRimData.apply_sex[${inx}].active`]:true,
      sex: id
    })
    this.publicS();
    }
  },
  age:function(e){
    let inx = e.currentTarget.dataset.in
    let id = e.currentTarget.dataset.id
    console.log(id, this.data.age)
    if (id != this.data.age){
    this.data.sportRimData.apply_age.forEach((item, index) => {
      let key = `sportRimData.apply_age[${index}].active`
      this.setData({
        [key]: false
      })
    })
    this.setData({
      [`sportRimData.apply_age[${inx}].active`]: true,
      age: id
    })
      this.publicS();
    }

    
  },
  goal:function(e){
    let inx = e.currentTarget.dataset.in
    let id = e.currentTarget.dataset.id
    if (id != this.data.goal){
    this.data.sportRimData.label.forEach((item, index) => {
      let key = `sportRimData.label[${index}].active`
      this.setData({
        [key]: false
      })
    })
    this.setData({
      [`sportRimData.label[${inx}].active`]: true,
      goal: id
    })
    this.publicS();
    }
  },
  publicS:function(){
    let data = {
      apply_sex:this.data.sex,
      apply_age: this.data.age,
      label_id: this.data.goal,
    }
    api.goal(data,(res)=>{
      let classS = 'sportRimData.cate'
      console.log(res)
      this.setData({
        [classS]: res.data.result.info,
        types: res.data.result.type
      })
    })
  },
  url:function(e){
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/indexs/channel/channel?id=${id}`,
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})